//
//  TalksInfoViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/14.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TalksInfoViewController.h"
#import "TalksInfoTableViewCell.h"
#import "TalksInfoHeaderView.h"
#import "PraiseModel.h"

@interface TalksInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UITextField * sayTextFiled;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, strong)TalksInfoHeaderView * headerView;
@property (nonatomic, strong)NSArray<PraiseModel *> * praiseModelArray;
@property (nonatomic, strong)NSDictionary * dict;
@property (nonatomic, copy)NSString * note_id;

@end

@implementation TalksInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [PraiseModel praiseModelWithType:self.ID WithSucess:^(NSArray<PraiseModel *> *array) {
       
        self.praiseModelArray = array;
    } error:^{
        
    }];
    
    NSDictionary * dict = @{@"note_id":self.ID};
    
    [[NetworkTool sharedTools] POST:@"/index/Common/noteDetail" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"res===%@",responseObject);
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
 
            self.dict = responseObject[@"data"];
            self.note_id = dict[@"note_id"];
//            NSLog(@"dict===%@",self.dict);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    self.headerView.dict = dict;
}

- (void)setPraiseModelArray:(NSArray<PraiseModel *> *)praiseModelArray{
    
    _praiseModelArray = praiseModelArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setupNav{
    
    self.title = @"话题详情页";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 64 - 35 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 90 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[TalksInfoTableViewCell class] forCellReuseIdentifier:@"TalksInfoListCell"];
    [self.tableView registerClass:[TalksInfoHeaderView class] forHeaderFooterViewReuseIdentifier:@"TalksInfoHeaderView"];
    
    [self.view addSubview:self.tableView];
    
    
    UITextField * sayTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 5 * SCREENH6, 260 * SCREENH6, 30 * SCREENH6)];
    
    sayTextFiled.borderStyle = UITextBorderStyleLine;
    sayTextFiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    sayTextFiled.tintColor = [UIColor whiteColor];
    sayTextFiled.layer.borderColor = [ColorWithRGB(0xE6E6E6) CGColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    sayTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请留下你的神评论" attributes:attrs];
    sayTextFiled.textColor = [UIColor blackColor];
    sayTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    sayTextFiled.returnKeyType = UIReturnKeyDone;
    //    sayTextFiled.delegate = self;
    
    self.sayTextFiled = sayTextFiled;
    
    [self.view addSubview:self.sayTextFiled];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"确认" forState:UIControlStateNormal];
    [sureButton setTitleColor:MAINCOLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.sayTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30 * SCREENH6);
        make.bottom.mas_equalTo(self.view).offset(-5 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.width.mas_equalTo(310 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view);
        make.leading.mas_equalTo(self.sayTextFiled.mas_trailing);
        make.centerY.height.mas_equalTo(self.sayTextFiled);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.praiseModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TalksInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TalksInfoListCell" forIndexPath:indexPath];
    
    cell.model = self.praiseModelArray[indexPath.row];
//    cell.titleLabel.text = self.showStringArray[indexPath.row];
//    cell.iconImageView.image = ImageNamed(self.imageNameArray[indexPath.row]);
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 323 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
     self.headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"TalksInfoHeaderView"];
    
//    self.headerView.dict = self.dict;
    
    return self.headerView;
}


- (void)clickSureButton{
    
    SHOWHUD;
    
    NSString * user_id = DEF_GET_OBJECT(@"user_id");
    NSString * token = DEF_GET_OBJECT(@"token");
    
    NSLog(@"==%@",token);
    
    NSDictionary * dict = @{@"user_id":user_id,@"note_id":self.note_id,@"content":self.sayTextFiled.text,@"token":token};
    
    
    [[NetworkTool sharedTools] POST:@"/index/Common/comment" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        HIDDENHUD;
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你，评论成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
//                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
//                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        HIDDENHUD;
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
